﻿using Orchard.Environment.Extensions;
using Orchard.Data.Migration;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;

namespace FruitCake.SimpleGallery.Migrations {

    [OrchardFeature("FruitCake.SimpleGallery")]
    public class SimpleGalleryMigrations : DataMigrationImpl {

        public int Create()
        {
            SchemaBuilder.CreateTable(
                "SimpleGalleryPartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<string>("Path", c => c.WithLength(2048))
                    .Column<int>("FrameWidth", c => c.NotNull().WithDefault(0))
                    .Column<int>("FrameHeight", c => c.NotNull().WithDefault(0))
                    .Column<int>("PageSize", c => c.NotNull().WithDefault(20))
                    .Column<int>("PagerMaxItems", c => c.NotNull().WithDefault(10))
                    .Column<bool>("EnableThumbnail", c => c.NotNull().WithDefault(false))
                    .Column<string>("TitleMode", c => c.WithLength(35).Nullable().WithDefault(Models.TitleMode.None.ToString()))
                    .Column<string>("TitleSeparator", c => c.WithLength(5).Nullable().WithDefault("_"))
                    .Column<string>("LinkMode", c => c.WithLength(35).Nullable().WithDefault(Models.LinkMode.LightBox.ToString())));
                    
            ContentDefinitionManager.AlterPartDefinition("SimpleGalleryPart", part => part.Attachable());

            ContentDefinitionManager.AlterTypeDefinition("SimpleGallery",
                builder => builder                    
                    .WithPart("CommonPart")
                    .WithPart("TitlePart")
                    .WithPart("AutoroutePart", ctx => ctx
                        .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                        .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                        .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Title', Pattern: '{Content.Slug}', Description: 'Gallery title'}]")
                        .WithSetting("AutorouteSettings.DefaultPatternIndex", "0"))
                    .WithPart("SimpleGalleryPart")
                    .WithPart("NavigationPart")
                    .DisplayedAs("SimpleGallery")
                    .Creatable()
                    .Draftable()
            );

            return 5;
        }

        public int UpdateFrom1()         
        {
            SchemaBuilder.AlterTable("SimpleGalleryPartRecord", table =>
                {
                    table.AddColumn<int>("FrameWidth", c => c.NotNull().WithDefault(0));
                    table.AddColumn<int>("FrameHeight", c => c.NotNull().WithDefault(0));
                    table.AddColumn<bool>("KeepAspectRatio", c => c.NotNull().WithDefault(true));
                    table.AddColumn<bool>("StretchImage", c => c.NotNull().WithDefault(true));
                    table.AddColumn<int>("PageSize", c => c.NotNull().WithDefault(0));
                });

            return 2;
        }

        public int UpdateFrom2()
        {
            SchemaBuilder.AlterTable("SimpleGalleryPartRecord", table =>
            {
                table.DropColumn("KeepAspectRatio");
                table.DropColumn("StretchImage");
                table.AddColumn<bool>("EnableThumbnail", c => c.NotNull().WithDefault(false));
                table.AddColumn<string>("TitleMode", c => c.WithLength(35).Nullable().WithDefault(Models.TitleMode.None.ToString()));
                table.AddColumn<string>("LinkMode", c => c.WithLength(35).Nullable().WithDefault(Models.LinkMode.LightBox.ToString()));
            });

            return 3;
        }

        public int UpdateFrom3()
        {
            SchemaBuilder.AlterTable("SimpleGalleryPartRecord", table =>
                table.AddColumn<string>("TitleSeparator", c => c.WithLength(5).Nullable().WithDefault("_")));
            
            return 4;
        }

        public int UpdateFrom4()
        {
            SchemaBuilder.AlterTable("SimpleGalleryPartRecord", table =>
                table.AddColumn<int>("PagerMaxItems", c => c.NotNull().WithDefault(10)));
                    
            return 5;
        }
    }

}